home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Text⁄Files
/
File List 14
/
File.c
< prev
next >
Wrap
Text File
|
1990-09-14
|
6KB
|
284 lines
/*
FileList 1.4
"File.c"
*/
#include "Main.h"
#include "Utilities.h"
#include "Stack.h"
#include "Search.h"
#include "File.h"
typedef struct { /* Header structure of file */
long version; /* File version */
long type; /* 'FILI' */
long creator; /* 'FILI' */
long infosize; /* Size (bytes) of info */
long infocount; /* Number of records */
long volumecount; /* Number of volumes */
long filecount; /* Number of files */
short volumesort; /* How volumes are sorted */
short filesort; /* How files are sorted */
long filler[8];
} Header;
/* ----- Copy selection to clipboard ------------------------------------ */
void CopySelection (register WindowDataPtr w)
{
register unsigned char s[256];
register unsigned char buf[512];
register unsigned char *p;
register unsigned short n;
StringHandle str;
if (!w || ((WindowPeek)w)->windowKind < 0 || w->select == -1)
return;
ZeroScrap();
p = buf;
if (!Tabs && (str = GetString(w->header))) {
BlockMove(*str + 1, p, n = **str);
p += n;
*p++ = '\r';
}
(*(w->string))(w->select, s);
BlockMove(s + 1, p, n = *s);
p += n;
*p++ = '\r';
PutScrap(p - buf, 'TEXT', buf);
}
/* ----- Save text of window -------------------------------------------- */
void DoSaveText (register WindowDataPtr w)
{
register short err;
register long i;
StringHandle str;
SFReply sfr;
short r;
long count;
Point where;
unsigned char s[256];
if (!w || (((WindowPeek)w)->windowKind < 0))
return;
GetDlogOrigin(putDlgID, &where);
if (w->vrefnum) {
SetVol(0L, w->vrefnum);
BlockMove(w->fname, s, w->fname[0] + 1L);
} else {
str = GetString(w->title);
BlockMove(*str, s, **str + 1L);
}
HLock(str = GetString(SAVE_TEXT_AS));
SFPutFile(where, *str, s, SFOutlineHook, &sfr);
HUnlock(str);
if (!sfr.good)
return;
FSDelete(sfr.fName, sfr.vRefNum);
if ((err = Create(sfr.fName, sfr.vRefNum, TextCreator, 'TEXT')) ||
(err = FSOpen(sfr.fName, sfr.vRefNum, &r)))
goto done2;
SetWatch();
if (!Tabs && (str = GetString(w->header))) {
BlockMove(*str, s, **str + 1L);
Append(s, CrStr);
count = *s;
if (err = FSWrite(r, &count, s + 1))
goto done1;
}
for (i = 0; i < w->count; i++) {
(*(w->string))(i, s);
Append(s, CrStr);
count = *s;
if (err = FSWrite(r, &count, s + 1))
goto done1;
}
done1:
FSClose(r);
FlushVol(0L, sfr.vRefNum);
InitCursor();
done2:
if (err)
Message(ERR_DISK, err);
else {
w->vrefnum = sfr.vRefNum;
BlockMove(sfr.fName, w->fname, sfr.fName[0] + 1L);
}
}
/* ----- Save all records ---------------------------------------------- */
Boolean DoSave (register Boolean ask)
{
register short err;
register long i;
StringHandle str;
SFReply sfr;
short r;
long count;
Point where;
Header header;
unsigned char s[256];
if (VrefNum) {
SetVol(0L, VrefNum);
sfr.vRefNum = VrefNum;
BlockMove(Fname, sfr.fName, Fname[0] + 1L);
BlockMove(Fname, s, Fname[0] + 1L);
} else {
ask = TRUE;
str = GetString(SAVENAME);
BlockMove(*str, s, **str + 1L);
}
if (ask) {
GetDlogOrigin (putDlgID, &where);
HLock(str = GetString(SAVE_FILE_AS));
SFPutFile(where, *str, s, SFOutlineHook, &sfr);
HUnlock(str);
if (!sfr.good)
return FALSE; /* Cancel */
}
if (VolumeData.select != -1L)
Select(&VolumeData, VolumeData.select, FALSE);
if (FileData.select != -1L)
Select(&FileData, FileData.select, FALSE);
if ((err = FSOpen(sfr.fName, sfr.vRefNum, &r)) == fnfErr) {
if (err = Create(sfr.fName, sfr.vRefNum, Creator, Creator))
goto done2;
err = FSOpen(sfr.fName, sfr.vRefNum, &r);
}
if (err)
goto done2;
SetWatch();
count = sizeof(header);
FillMemory((unsigned char *)&header, count, 0);
header.version = VERSION;
header.type = Creator;
header.creator = Creator;
header.infosize = InfoSize;
header.infocount = InfoCount;
header.volumecount = VolumeData.count;
header.filecount = FileData.count;
header.volumesort = VolumeData.sorted;
header.filesort = FileData.sorted;
count = sizeof(header);
if (err = FSWrite(r, &count, &header))
goto done1;
count = InfoSize;
if (err = FSWrite(r, &count, InfoBase))
goto done1;
count = VolumeData.count * sizeof(long);
if (err = FSWrite(r, &count, VolumeData.base))
goto done1;
count = FileData.count * sizeof(long);
err = FSWrite(r, &count, FileData.base);
done1:
FSClose(r);
FlushVol(0L, sfr.vRefNum);
InitCursor();
done2:
if (err)
Message(ERR_DISK, err);
else {
Dirty = FALSE;
VrefNum = sfr.vRefNum;
BlockMove(sfr.fName, Fname , sfr.fName[0] + 1L);
}
Update();
return TRUE;
}
/* ----- Read all records ---------------------------------------------- */
void Read (
short vRefNum,
unsigned char *fName)
{
register short err;
short r;
long count;
Header header;
if (err = FSOpen(fName, vRefNum, &r))
goto done2;
SetWatch();
count = sizeof(header);
if (err = FSRead(r, &count, &header))
goto done1;
if (header.version != VERSION ||
header.type != Creator ||
header.creator != Creator) {
err = 9999;
goto done1;
}
if (header.infosize > InfoMax ||
header.volumecount > VolumeData.max ||
header.filecount > FileData.max) {
err = 10000;
goto done1;
}
InfoSize = header.infosize;
InfoCount = header.infocount;
VolumeData.count = header.volumecount;
FileData.count = header.filecount;
count = InfoSize;
if (err = FSRead(r, &count, InfoBase))
goto done1;
count = VolumeData.count * sizeof(long);
if (err = FSRead(r, &count, VolumeData.base))
goto done1;
count = FileData.count * sizeof(long);
if (err = FSRead(r, &count, FileData.base))
goto done1;
VolumeData.sorted = header.volumesort;
FileData.sorted = header.filesort;
done1:
FSClose(r);
InitCursor();
done2:
if (err)
Message(ERR_DISK, err);
else {
Dirty = FALSE;
VrefNum = vRefNum;
BlockMove(fName, Fname , fName[0] + 1L);
}
}
/* ----- Open command -------------------------------------------------- */
void DoOpen (void)
{
SFReply sfr;
Point where;
if (SaveBefore()) {
ClearSorted(&FileData);
ClearSorted(&VolumeData);
GetDlogOrigin (getDlgID, &where);
SFGetFile (where, EmptyStr, 0L, 1, &Creator, SFOutlineHook, &sfr);
if (sfr.good) {
Read(sfr.vRefNum, sfr.fName);
Update();
VolumeData.select = FileData.select = -1L;
if (FileData.sorted)
CheckItem(FileData.sortmenu, FileData.sorted, TRUE);
if (VolumeData.sorted)
CheckItem(VolumeData.sortmenu, VolumeData.sorted, TRUE);
}
}
}